package com.threeonemeeting.activity;

import java.util.Map;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.snda.lib.http.HttpUtil;
import com.snda.lib.http.ICallBack;
import com.snda.lib.http.TaskManager;
import com.threeonemeeting.Const;
import com.threeonemeeting.DataCenter;
import com.threeonemeeting.R;
import com.threeonemeeting.adapter.MeetingInfoListAdapter;
import com.threeonemeeting.list.FullMeetingInfoList;
import com.threeonemeeting.list.MeetingExpressList;
import com.threeonemeeting.model.FullMeetingInfo;
import com.threeonemeeting.model.MeetingExpress;
import com.threeonemeeting.model.MeetingInfo;
import com.threeonemeeting.tab.BaseTabActivity;
import com.threeonemeeting.tab.MultiActivityTab;
import com.threeonemeeting.task.GetExpressListTask;
import com.threeonemeeting.task.GetMeetingListInExpressTask;
import com.threeonemeeting.task.HttpConst;
import com.threeonemeeting.util.MiscUtil;

public class MeetingExpressListActivity extends BaseTabActivity implements
		OnItemClickListener, ICallBack {

	protected int nExpressId = 0;
	private MeetingInfoListAdapter mAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_meeting_express_list);

		MiscUtil.setBackBtnListener(this);
		MiscUtil.showLoadingLayout(this);

		// ListView
		ListView listViewExpress = (ListView) findViewById(R.id.lv_express);
		mAdapter = new MeetingInfoListAdapter(this);
		listViewExpress.setAdapter(mAdapter);
		listViewExpress.setOnItemClickListener(this);

		// 选择其余会讯的按钮
		Button btnSelectExpress = (Button) findViewById(R.id.btn_changeexpress);
		OnClickListener selectExpressBtnClickListener = new OnClickListener() {

			public void onClick(View v) {

				final DataCenter dataCenter = DataCenter.getInstance();
				final String[] expresses = new String[dataCenter.listExpress
						.size()];
				for (int i = 0; i < dataCenter.listExpress.size(); i++) {

					MeetingExpress expressInfo = dataCenter.listExpress.get(i);
					if (expressInfo == null)
						continue;

					expresses[i] = expressInfo.strTitle;
				}

				new AlertDialog.Builder(
						((MultiActivityTab) MeetingExpressListActivity.this
								.getParent()))
						.setTitle("选择其余会讯")
						.setItems(expresses,
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int which) {

										// 打开对应的会讯专题
										MeetingExpress expressInfo = dataCenter.listExpress
												.get(which);
										if (expressInfo == null)
											return;

										GetMeetingListInExpressTask task = new GetMeetingListInExpressTask(
												MeetingExpressListActivity.this,
												MeetingExpressListActivity.this);
										Bundle params = new Bundle();
										params.putString("mid",
												String.valueOf(expressInfo.nId));
										TaskManager.addTask(task, params, true);

										TextView title = (TextView) findViewById(R.id.express_title);
										if (title != null) {
											title.setText(expressInfo.strTitle);
										}
									}
								}).create().show();

			}
		};
		if (btnSelectExpress != null) {
			btnSelectExpress.setOnClickListener(selectExpressBtnClickListener);
		}

		// 发送请求
		GetExpressListTask task = new GetExpressListTask(
				MeetingExpressListActivity.this,
				MeetingExpressListActivity.this);
		TaskManager.addTask(task, null, true);

	}

	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

		int nIndex = arg2;

		MeetingInfo mInfo = mAdapter.getItem(nIndex);
		if (mInfo == null)
			return;

		int nId = mInfo.nId;

		// 跳转到会议详情页面
		Bundle b = new Bundle();
		b.putInt("id", nId);
		startActivity("MeetingDetailActivity", b);
	}

	public void doCallBack(Map<String, Object> map) {
		Integer msgType = (Integer) map.get(HttpConst.KEY_TASK_TYPE);
		if (msgType == null) {
			return;
		}

		Integer nError = (Integer) map.get(HttpUtil.KEY_ERROR_CODE);
		if (nError == null || nError != HttpUtil.ERROR_SUCCESS) {
			return;
		}

		// 获取会讯列表
		if (msgType == HttpConst.TASKTYPE_GETMEETINGEXPRESSLIST) {

			MeetingExpressList listExpress = (MeetingExpressList) map
					.get(Const.Keys.KEY_LIST);
			if (listExpress == null)
				return;

			MeetingExpress maxIdExpress = listExpress.getMaxIdMeetingExpress();
			if (maxIdExpress == null)
				return;

			nExpressId = maxIdExpress.nId;

			TextView title = (TextView) findViewById(R.id.express_title);
			if (title != null && maxIdExpress != null) {
				title.setText(maxIdExpress.strTitle);
			}

			GetMeetingListInExpressTask task = new GetMeetingListInExpressTask(
					this, this);
			Bundle params = new Bundle();
			params.putString("mid", String.valueOf(maxIdExpress.nId));
			TaskManager.addTask(task, params, true);

		} else if (msgType == HttpConst.TASKTYPE_GETEXPRESSMEETINLIST) {

			FullMeetingInfoList list = (FullMeetingInfoList) map.get("list");
			if (list == null)
				return;

			MiscUtil.showListLayout(this, list.size());

			mAdapter.clear();

			for (int i = 0; i < list.size(); i++) {
				FullMeetingInfo fullInfo = list.get(i);
				if (fullInfo == null)
					continue;

				MeetingInfo info = new MeetingInfo(fullInfo);
				mAdapter.add(info);
			}

			mAdapter.notifyDataSetChanged();
		}
	}
}
